package com.ddstation.app.appeal.controller;

import com.ddstation.app.appeal.model.AppAppealModel;
import com.ddstation.app.appeal.service.AppAppealService;
import com.ddstation.appeal.service.AppealService;
import com.ddstation.common.entity.DdPictureEntity;
import com.ddstation.common.mapper.DdPictureMapper;
import com.ddstation.common.util.AliCloudOssUtil;
import com.ddstation.common.util.CheckUtil;
import com.ddstation.crud.entity.DtInvsIllegalAppealEntity;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import java.util.*;

@Controller @Lazy
public class AppAppealController {

	@Inject private AppealService appealService;
	@Inject private AppAppealService appAppealService;
	@Inject private DdPictureMapper ddPictureMapper;

	/**
	 * app警报列表
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_appeal.do/list", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> invsAppealListEvent(
			@RequestBody Map<String, Object> params){

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);

		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("type"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		Integer type = Integer.parseInt(params.get("type").toString());
		Integer state = CheckUtil.isEmpty(params.get("state"))?null:Integer.parseInt(params.get("state").toString());
		Integer appealSt = null;
		if (state != null) {
			if (state.intValue() == 0 || state.intValue() == 1) {
				appealSt = 0;
			} else if (state.intValue() == 2) {
				appealSt = 2;
			} else if (state.intValue() == 3) {
				appealSt = -1;
			} else if (state.intValue() == 4) {
				appealSt = 1;
			} else if (state.intValue() == 5) {
				appealSt = 10;
			}
		}
		List<AppAppealModel> entityList = appAppealService.queryAppealList(institutionId,appealSt,type);

		for (AppAppealModel model : entityList) {

			List<String> thumbImgList = new ArrayList<String>();
			List<String> originImgList = new ArrayList<String>();
			if (model.getAppealAtta()!=null) {
				for (String m_id:model.getAppealAtta().split(",")) {
					if (!CheckUtil.isEmpty(m_id)) {
						DdPictureEntity picture = ddPictureMapper.selectOne(m_id);
						if (picture.getPath().endsWith("pdf") || picture.getPath().endsWith("PDF")
								|| picture.getPath().endsWith("xls") || picture.getPath().endsWith("xlsx")
								|| picture.getPath().endsWith("XLS") || picture.getPath().endsWith("XLSX")
								|| picture.getPath().endsWith("doc") || picture.getPath().endsWith("docx")
								|| picture.getPath().endsWith("DOC") || picture.getPath().endsWith("DOCX")) {
							continue;
						}
						thumbImgList.add(AliCloudOssUtil.getThumbImgUrl(null, picture.getPath(), "@100h"));
						originImgList.add(AliCloudOssUtil.getThumbImgUrl(null, picture.getPath(), "@800h"));
						model.setThumbImgList(thumbImgList);
						model.setOriginImgList(originImgList);
					}
				}
			}
		}
		retMap.put("list",entityList);
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;
	}

	/**
	 * (医疗废弃物处理登记)上传记录
	 * @param params
	 * @return
	 */
	@RequestMapping(value="appinvs_invs_appeal.do/submit", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> invsWushuiCheckSaveEvent(
			@RequestBody Map<String, Object> params) {

		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("doctorId"))
				|| CheckUtil.isEmpty(params.get("id"))
				|| CheckUtil.isEmpty(params.get("remarkText"))) {
			return retMap;
		}
		String institutionId = params.get("institutionId").toString();
		String illegalId = params.get("id").toString();
		Integer doctorId = Integer.parseInt(params.get("doctorId").toString());
		String remarkText = params.get("remarkText").toString();
		String attachmentId = CheckUtil.isEmpty(params.get("attachmentId"))? null : params.get("attachmentId").toString();

		DtInvsIllegalAppealEntity entity = new DtInvsIllegalAppealEntity();
		entity.setIllegalId(illegalId);
		entity.setAppeal(remarkText);
		entity.setAppealAtta(attachmentId);
		entity.setAppealTm(new Date());
		entity.setAppealDoctorId(doctorId);
		entity.setAppealSt(1);
        entity.setInstitutionId(institutionId);
		appealService.updateAppeal(entity);
		retMap.put("errCode", 0);
		retMap.put("errorMsg", "");
		return retMap;
	}

	@RequestMapping(value="appinvs_invs_appeal.do/check_type", method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> checkapealType(
			@RequestBody Map<String, Object> params) {
		Map<String, Object> retMap = new HashMap<String, Object>();
		retMap.put("errCode", -1);
		if (params.isEmpty() || CheckUtil.isEmpty(params.get("institutionId"))
				|| CheckUtil.isEmpty(params.get("illegalId"))) {
			return retMap;
		}
		retMap.put("illegalDetail", appealService.getIllegalDetailById(Integer.parseInt(params.get("illegalId").toString())));
		retMap.put("errCode", 0);
		return retMap;
	}
}
